//
//  ZXMainViewController.swift
//  PandaEye_Weibo
//
//  Created by Xuan Zhang on 16/6/14.
//  Copyright © 2016年 Xuan Zhang. All rights reserved.
//

import UIKit

class ZXMainViewController: UITabBarController {
    lazy var composeButton = {
        ()->UIButton in
        print(self)
        return  UIButton(title: "", image: "tabbar_compose_icon_add", backgroundImage: "tabbar_compose_button")
    }()
//    lazy var composeButton = UIButton(title: "",image: "tabbar_compose_icon_add", backgroundImage: "tabbar_compose_button")
    override func viewDidLoad() {
        super.viewDidLoad()
        setupChildViewControllers()
        //以下也报错了
//        composeButton.addTarget(self, action:"compose:",forControlEvents: .TouchUpInside)

    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        tabBar.bringSubviewToFront(composeButton)
    }
    
}

///Users/xuanzhang/Documents/weibo_pandaeye/PandaEye_Weibo/PandaEye_Weibo/Class/Module/Main/Controller/ZXMainViewController.swift:14:134: Cannot convert value of type 'NSObject -> () -> ZXMainViewController' to expected argument type 'AnyObject?'

extension ZXMainViewController {
    private func setupChildViewControllers(){
//        var temp1 = self
        //首页
        setupChildViewController(ZXHomeTableViewController(), imageName: "tabbar_home", title: "首页")
        //消息
        setupChildViewController(ZXMessageTableViewController(), imageName: "tabbar_message_center", title: "消息")
        //占位
        setupChildViewController(UIViewController(), imageName: nil, title: "")
        //发现
        setupChildViewController(ZXDiscoverTableViewController(), imageName: "tabbar_discover", title: "发现")
        //我
        setupChildViewController( ZXProfileTableViewController(), imageName: "tabbar_profile", title: "我")
        addComposeButton()
        setTabBar()
    }
    ///  设置子控制器
    ///
    ///  - parameter controller: 控制器
    ///  - parameter imageName:  图片名称
    ///  - parameter title:      标题
    private func setupChildViewController(controller:UIViewController,imageName:String?,title:String){
        if let imageName = imageName {
            controller.tabBarItem.image = UIImage(named:imageName)?.imageWithRenderingMode(.AlwaysOriginal)
            controller.tabBarItem.selectedImage = UIImage(named:imageName+"_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        }
        
        controller.title = title
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Selected)
        addChildViewController(UINavigationController(rootViewController: controller))
    }
    
    private func addComposeButton(){
        tabBar.addSubview(composeButton)
        
//        composeButton.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
//        composeButton.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Selected)
//        composeButton.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
//        composeButton.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        let tabBarItemWidth = tabBar.bounds.size.width/5
        let composeFrame = CGRectInset(tabBar.bounds, 2*tabBarItemWidth, 0)
        composeButton.frame = composeFrame
        tabBar.bringSubviewToFront(composeButton)
    }
    
    //设置tabbar
    private func setTabBar(){
        //设置tabbar的阴影和背景颜色
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
        tabBar.shadowImage = UIImage.createADotImage(UIColor(white: 0.9, alpha: 1.0))
        
    }
    
    private func compose(button:UIButton){
        print("compose")
    }
    
    
    
}
